﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OnlineShop.Models;
using OnlineShop.Common;
using OnlineShop.Models.ViewModels;

namespace OnlineShop.Controllers
{
    public class AccountController : BaseController
    {
        #region Repository
        private IOnlineShopRepository repository;

        public AccountController():this(new EFOnlineShopRepository())
        {
        }

        public AccountController(EFOnlineShopRepository eFOnlineShopRepository)
        {
            this.repository = eFOnlineShopRepository;
        }

        #endregion

        //
        // GET: /Account/

        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Login(string returnUrl)
        {
            if (CurrentUser != null)
                return RedirectToHome();

            var model = new LoginModel();
            model.ReturnUrl = returnUrl;

            return View(model);
        }

        [HttpPost]
        public ActionResult Login(LoginModel model)
        {
            if (!this.ModelState.IsValid)
                return View(model);

            OSUser user = repository.ValidateUser(model.LoginName, model.Password.Encrypt());
            if (user == null)
                ModelState.AddModelError("Login", "Tên đăng nhập hoặc mật khẩu không đúng. Thử lại !");

            Session["OSUser"] = user;

            if (string.IsNullOrEmpty(model.ReturnUrl))
                return RedirectToHome();

            return Redirect(model.ReturnUrl);
        }

        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Register(AccountModel model)
        {
            if (CurrentUser != null)
                return RedirectToHome();

            if (!ModelState.IsValid)
                return View(model);

            OSUser user = repository.FindUserByLoginName(model.Email);
            if (user != null)
            {
                ModelState.AddModelError("Register", "Email được dùng đã tồn tại. Thử lại !");
                return View(model);
            }

            user = new OSUser()
            {
                DateRegister = DateTime.Now,
                Email = model.Email,
                Password = model.Password.Encrypt(),
                UserGroupId = 2,
                IsActived = true                
            };

            var userinfo = new UserInfo()
                {
                    Address1 = model.Address1,
                    Address2 = model.Address2,
                    City = model.City,
                    CountryCode = model.CountryCode,
                    FistName = model.FirstName,
                    LastName = model.LastName,
                    PhoneNumber = model.PhoneNumber,
                    StateProvince = model.StateProvince,
                    ZipPostalCode = model.ZipCode
                };

            user.UserInfoes.Add(userinfo);

            repository.AddUser(user);
            repository.CommitChanges();

            return RedirectToLogin();
        }

        public ActionResult Logout()
        {
            Session["OSUser"] = null;
            return RedirectToHome();
        }
    }
}
